package com.niit.gjl.firmhousenum;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class FirmHouseNumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable outv = new IntWritable();

    /**
     * Reduce阶段核心业务逻辑处理（相同的key一组数据调用一次reduce方法进行汇总）- 和统计单词数量逻辑一样
     *
     * @param key     公司, eg: 正顺地产...
     * @param values  出现次数,eg: 1,2...
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        int sum = 0;
        // 遍历values 进行累加汇总
        for (IntWritable value : values) {
            sum += value.get();
        }
        outv.set(sum);
        // 将结果写出
        context.write(key, outv);
    }
}
